import { ExtractJwt, Strategy } from 'passport-jwt';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable } from '@nestjs/common';

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor() {
    super({
      // 从认证头中提取JWT
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      // 是否忽略过期
      ignoreExpiration: false,
      // 秘钥
      secretOrKey: process.env.SECRET,
      // 传递请求到回调函数
      passReqToCallback: true,
    });
  }

  async validate(req, user: any) {
    // 从认证头中提取JWT
    // const token = ExtractJwt.fromAuthHeaderAsBearerToken()(req);

    return user;
  }
}
